//---------------------------------------------------------------------------
#pragma hdrstop

#include "Stdafx.h"
#include "Functions.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)

void Delay(DWORD DT)
{
	long tt;
	tt=GetTickCount();
	while(GetTickCount()-tt<DT)
	{
			Application->ProcessMessages();
			if( GetTickCount() - tt <= 0  )
			tt = GetTickCount();
	}
}

void AssignMap()
{
	for (int x = 0; x < 640; x++)
	{
		for (int y = 0; y < 480; y++)
		{
			if( Form1->Map->Canvas->Pixels[x][y] )
			{
				Swarm.ImMap[y][x] = false;
			}
			else
			{
				Swarm.ImMap[y][x] = true;
			}
		}
	}

}

void paint_StartEnd()
{
	Form1->Simu->Picture->Assign(Form1->Map->Picture->Bitmap);

	int radius = 5;
	Form1->Simu->Canvas->Brush->Color = clRed;
	Form1->Simu->Canvas->Pen->Color = clRed;
	Form1->Simu->Canvas->Brush->Style = bsSolid;
	Form1->Simu->Canvas->Ellipse(Swarm.Start.x-radius,Swarm.Start.y-radius,Swarm.Start.x+radius,Swarm.Start.y+radius);

	Form1->Simu->Canvas->Brush->Color = clGreen;
	Form1->Simu->Canvas->Pen->Color = clGreen;
	Form1->Simu->Canvas->Ellipse(Swarm.End[0].x-radius,Swarm.End[0].y-radius,Swarm.End[0].x+radius,Swarm.End[0].y+radius);

	Form1->Simu->Canvas->Brush->Color = clBlue;
	Form1->Simu->Canvas->Pen->Color = clBlue;
	Form1->Simu->Canvas->Ellipse(Swarm.End[1].x-radius,Swarm.End[1].y-radius,Swarm.End[1].x+radius,Swarm.End[1].y+radius);

	Form1->Simu->Canvas->Pen->Color = clPurple;
	Form1->Simu->Canvas->MoveTo(Swarm.End[0].x, Swarm.End[0].y);
	Form1->Simu->Canvas->LineTo(Swarm.End[1].x, Swarm.End[1].y);
}

void paint_All()
{
	paint_StartEnd();

	Form1->Simu->Canvas->Pen->Color = clBlue;
	for (int i = 0; i < ParticalNum; i++)
	{
		Form1->Simu->Canvas->MoveTo(Swarm.Partical[i].Pos[0].x, Swarm.Partical[i].Pos[0].y);
		for (int j = 1; j < PointNum; j++)
		{
			Form1->Simu->Canvas->LineTo(Swarm.Partical[i].Pos[j].x, Swarm.Partical[i].Pos[j].y);
		}
	}

	Form1->Simu->Canvas->Pen->Color = clRed;
	Form1->Simu->Canvas->MoveTo(Swarm.BestPos[0].x, Swarm.BestPos[0].y);
	for (int j = 1; j < PointNum; j++)
	{
		Form1->Simu->Canvas->LineTo(Swarm.BestPos[j].x, Swarm.BestPos[j].y);
	}
}

